import random
import time

import requests
import pymysql

# API地址和基本信息
# base_url = 'http://152.32.143.7:8080/api'
base_url = 'http://124.223.139.46:30001/api'
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
}
#预生产
# conn = pymysql.connect(host='sh-cynosdbmysql-grp-p0iv1cd6.sql.tencentcdb.com', port=23062, user='root',
#                        password='Moregold!23', database='card_platform', charset="utf8")


# 测服
conn = pymysql.connect(host='sh-cynosdbmysql-grp-dhfnikj6.sql.tencentcdb.com', port=21044, user='root',
                       password='Moregold!23', database='nexus', charset="utf8")


previous_invite_code = "9QKN7P"
for _ in range(3):
    # 生成234+10位数字随机手机号
    phone = '234' + f"{random.randint(1000000000, 9999999999)}"

    # 接口1：注册短信发送
    response = requests.post(
        f"{base_url}/users",
        headers=headers,
        data={'phone': phone}
    )
    response_data = response.json()

    # 接口2：注册
    response = requests.post(
        f"{base_url}/user/verify",
        headers=headers,
        json={
            "country": "Nigeria",
            "first_name": "123",
            "invite_code": previous_invite_code,
            "last_name": "456",
            "password": "qwe123",
            "promo_code": "",
            "type": "phone",
            "value": phone,
            "verification_code": "9999"
        }
    )
    response_data = response.json()
    previous_invite_code = response_data['invite_code']
    print(f"手机号：{phone}，邀请码：{previous_invite_code}")

    # 连接数据库
    try:
        with conn.cursor(pymysql.cursors.DictCursor) as cursor:
            # 执行查询
            cursor.execute(f"SELECT * FROM users WHERE name = {phone}")
            result = cursor.fetchone()
            if result:
                beneficial_company_id = result['beneficial_company_id']
                if beneficial_company_id == 93: #测服 93
                    # print(f"The beneficial_company_id for phone {phone} is 38.")
                    pass
                else:
                    print(f"{phone} 所属公司有误 , 所属公司id：{beneficial_company_id}.")
            else:
                print(f"No user found with phone {phone}.")
            time.sleep(2)
    except Exception as e:
        print(f"Error: {e}")
